package com.bluesky.jwf.component;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

import com.bluesky.javawebbrowser.domain.html.tags.TagType;
import com.bluesky.javawebbrowser.domain.html.tags.form.TextArea;
import com.bluesky.javawebbrowser.domain.html.tags.form.input.Text;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

public class TextBox extends Component implements StringField{
	public static enum TextMode {
		SingleLine, MultipleLine;
	}

	private TextMode textMode;
	private Text text;
	private TextArea textArea;

	public String getText() {
		String s;
		if (textMode == TextMode.SingleLine)
			s = text.getValue();
		else
			s = textArea.getText();
		if (s == null)
			s = "";
		return s;
	}

	public void setText(String text) {
		if (textMode == TextMode.SingleLine)
			this.text.setValue(text);
		else
			textArea.setText(text);
	}

	@Override
	public void init(String html) {

		super.init(html);

		text = (Text) loadByJwfClass("textbox");
		textArea = (TextArea) loadByJwfClass("textarea");

		setTextMode(TextMode.SingleLine);

	}

	@Override
	public void setId(String id) {
		super.setId(id);
		text.setName(getId() + "_textbox");
		textArea.setName(getId() + "_textbox");
	}

	@Override
	public void setPostBackData(String name, String value) {
		super.setPostBackData(name, value);

		if (name.equals("textbox"))
			setText(value);
	}

	public TextMode getTextMode() {
		return textMode;
	}

	public void setTextMode(TextMode textMode) {

		// store the text
		String s = getText();

		this.textMode = textMode;

		// reset the text once text mode is changed.
		setText(s);

		removeAllChildren();
		if (textMode == TextMode.SingleLine)
			addChild(text);
		else
			addChild(textArea);
	}

	public Integer getCols() {
		return textArea.getCols();
	}

	public void setCols(Integer cols) {

		textArea.setCols(cols);
	}

	public Integer getRows() {
		return textArea.getRows();
	}

	public void setRows(Integer rows) {
		textArea.setRows(rows);
	}

	@Override
	public String getString() {
		
		return getText();
	}

}
